package com.sms.monitor.server;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.os.Handler;
import android.os.IBinder;

import com.sms.monitor.constant.Constant;
import com.sms.monitor.utils.ULog;

public class SMSServer extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        MyContentObserver content = new MyContentObserver(new Handler(), this);
        // 注册短信变化监听
        this.getContentResolver().registerContentObserver(MyContentObserver.MMS_SMS_URI, true, content);

        ULog.d("onCreate");

        // 对应用之前的所发出去的短信，进行无视备注
        Cursor cursor = getContentResolver().query(MyContentObserver.MMS_SMS_URI, new String[] { "_id" }, "type=?",
                new String[] { "2" }, null);
        StringBuilder sb = new StringBuilder();
        while (cursor.moveToNext()) {
            sb.append(cursor.getInt(0)).append(",");
        }
        cursor.close();
        SharedPreferences spf = getSharedPreferences(Constant.KEY_SMS_TO_IDS, Context.MODE_PRIVATE);
        Editor editor = spf.edit();
        editor.putString(Constant.KEY_SMS_TO_IDS, sb.toString());
        editor.commit();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
